﻿
Imports System.Security.Cryptography

Namespace safty
    Public Class _hashalgorithm(Of T As HashAlgorithm, PD As poolDef)
        Private Shared _pool As poolDef = Nothing

        Public Shared Function hashsize(ByRef size As Int64) As Boolean
            Dim inst As T = Nothing
            If _pool.getfree(inst) Then
                size = inst.HashSize()
                _pool.release()
                Return True
            Else
                Return False
            End If
        End Function

        Public Shared Function hashsize() As Int64
            Dim rtn As Int64 = npos
            If hashsize(rtn) Then
                Return rtn
            Else
                Return npos
            End If
        End Function

        Public Shared Function signing(ByVal bs() As Byte,
                                       ByVal start As Int64, ByVal len As Int64,
                                       ByRef rtn() As Byte) As Boolean
            If bs Is Nothing OrElse bs.Length() < start + len Then
                Return False
            End If

            Dim inst As T = Nothing
            If _pool.getfree(inst) Then
                If isdebugmode() Then
                    assert(inst.CanReuseTransform())
                End If
                rtn = inst.ComputeHash(bs, start, len)
                _pool.release()
                Return True
            Else
                Return False
            End If
        End Function

        Public Shared Function signing(ByVal bs() As Byte, ByVal len As Int64, ByRef rtn() As Byte) As Boolean
            Return signing(bs, 0, len, rtn)
        End Function

        Public Shared Function signing(ByVal bs() As Byte, ByRef rtn() As Byte) As Boolean
            If bs Is Nothing Then
                Return False
            End If
            Return signing(bs, bs.Length(), rtn)
        End Function

        Public Shared Function signing(ByVal bs() As Byte, ByRef sign As String) As Boolean
            Dim rtn() As Byte = Nothing
            Return signing(bs, rtn) AndAlso convertor.tohexstring(rtn, sign)
        End Function

        Public Shared Function signing(ByVal bs() As Byte) As Byte()
            Dim rtn() As Byte = Nothing
            Return ifoper(signing(bs, rtn), rtn, Nothing)
        End Function

        Public Shared Function signingStr(ByVal bs() As Byte) As String
            Dim s As String = Nothing
            Return ifoper(signing(bs, s), s, Nothing)
        End Function

        Public Shared Function signing(ByVal s As String, ByRef rtn() As Byte, _
                                   Optional ByVal e As Text.Encoding = Nothing) As Boolean
            If e Is Nothing Then
                e = encoding.utf8()
            End If
            Return signing(e.GetBytes(s), rtn)
        End Function

        Public Shared Function signing(ByVal s As String, ByRef sign As String, _
                                   Optional ByVal e As Text.Encoding = Nothing) As Boolean
            Dim rtn() As Byte = Nothing
            Return signing(s, rtn, e) AndAlso convertor.tohexstring(rtn, sign)
        End Function

        Public Shared Function signing(ByVal s As String, Optional ByVal e As Text.Encoding = Nothing) As Byte()
            Dim rtn() As Byte = Nothing
            Return ifoper(signing(s, rtn, e), rtn, Nothing)
        End Function

        Public Shared Function signingStr(ByVal s As String, Optional ByVal e As Text.Encoding = Nothing) As String
            Dim rtn As String = Nothing
            Return ifoper(signing(s, rtn, e), rtn, Nothing)
        End Function

        Public Shared Function signing(Of T2)(ByVal i As T2, ByRef rtn() As Byte) As Boolean
            Return signing(Convert.ToString(i), rtn)
        End Function

        Public Shared Function signing(Of T2)(ByVal i As T2, ByRef sign As String) As Boolean
            Dim rtn() As Byte = Nothing
            Return signing(i, rtn) AndAlso convertor.tohexstring(rtn, sign)
        End Function

        Public Shared Function signing(Of T2)(ByVal i As T2) As Byte()
            Dim rtn() As Byte = Nothing
            Return ifoper(signing(i, rtn), rtn, Nothing)
        End Function

        Public Shared Function signingStr(Of T2)(ByVal i As T2) As String
            Dim rtn As String = Nothing
            Return ifoper(signing(i, rtn), rtn, Nothing)
        End Function

        Shared Sub New()
            _pool = alloc(Of PD)()
        End Sub

        Public MustInherit Class poolDef
            Inherits pool(Of T, _true, _false)

            Protected MustOverride Overrides Function create() As T

            Protected Overrides Sub unalloc(ByRef inst As T)
                inst.Clear()
                MyBase.unalloc(inst)
            End Sub
        End Class
    End Class

    Public Class md5Calculator
        Inherits _hashalgorithm(Of MD5, md5poolDef)

        Public Class md5poolDef
            Inherits poolDef

            Protected Overrides Function create() As MD5
                Return MD5.Create()
            End Function
        End Class
    End Class

    Public Class sha1Calculator
        Inherits _hashalgorithm(Of SHA1, sha1pooldef)

        Public Class sha1pooldef
            Inherits poolDef

            Protected Overrides Function create() As SHA1
                Return SHA1.Create()
            End Function
        End Class
    End Class

    Public Class sha256Calculator
        Inherits _hashalgorithm(Of SHA256, sha256pooldef)

        Public Class sha256pooldef
            Inherits poolDef

            Protected Overrides Function create() As SHA256
                Return SHA256.Create()
            End Function
        End Class
    End Class

    Public Class sha384Calculator
        Inherits _hashalgorithm(Of SHA384, sha384pooldef)

        Public Class sha384pooldef
            Inherits poolDef

            Protected Overrides Function create() As SHA384
                Return SHA384.Create()
            End Function
        End Class
    End Class

    Public Class sha512Calculator
        Inherits _hashalgorithm(Of SHA512, sha512pooldef)

        Public Class sha512pooldef
            Inherits poolDef

            Protected Overrides Function create() As SHA512
                Return SHA512.Create()
            End Function
        End Class
    End Class

    Public Class ripemd160Calculator
        Inherits _hashalgorithm(Of RIPEMD160Managed, ripemd160pooldef)

        Public Class ripemd160pooldef
            Inherits poolDef

            Protected Overrides Function create() As RIPEMD160Managed
                Return New RIPEMD160Managed()
            End Function
        End Class
    End Class
End Namespace
